<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>登录</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        #login {
            width: 600px;
            border: #4476A7 1px solid;
            margin: 100px auto;
            padding: 15px 20px;
            box-shadow: 20px 20px 1px 1px aliceblue;
        }
    </style>
</head>
<body>

<div id="login">
    <form class="layui-form" action="" lay-filter="example">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入用户名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">验证码</label>
            <div class="layui-input-inline">
                <input type="text" name="capchar" autocomplete="off" placeholder="请输入验证码" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <img src="captcha" style="width:85px;height: 38px" id="cap"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">15天内免登录</label>
            <div class="layui-input-block">
                <input type="checkbox" id="loginwithoutpwd" name="choose" lay-skin="switch" lay-text="ON|OFF">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="loginBtn">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>

        </div>
    </form>
</div>
<!-- 你的 HTML 代码 -->

<script src="./layui/layui.js"></script>
<script>
    layui.use(function () {

        var form = layui.form;
        var $ = layui.$;
        var layer = layui.layer;
        $("#cap").click(function () {
            path = "/ssm2202/captcha?" + new Date().getTime();
            $(this).attr("src", path);
        });

        //监听提交
        form.on('submit(loginBtn)', function (data) {
            var val = $("#loginwithoutpwd").val();

            //发ajax请求
            $.ajax({
                url: "/ssm2202/user/login",
                data: data.field,
                // 如果请求体中要放json，使用如下两行代码
                // contentType:"application/json",
                // data:JSON.stringify(data.field),
                type: "post",
                success: function (result) {
                    console.log(result);
                    // 登录成功进行页面跳转
                    if (result.code == "1") {
                        window.location = "/ssm2202/plateform.html";
                    } else {
                        // 登录失败给出提示信息
                        layer.alert(result.message, {
                            title: '登录失败'
                        });
                    }
                }
            });
            //防止刷新页面,取消表单地默认行为
            return false;
        });
    });
</script>
</body>
</html>
